///**************************************************************
//* Copyright (C) 2018 www.eipflow.com 孙泽伟版权所有(盗版必究)
//*
//* 作者: 孙泽伟(QQ 1039318332)
//* 创建时间: 2018/10/30 22:40:15
//* 文件名: 
//* 描述: 
//* 
//* 修改历史
//* 修改人：
//* 时间：
//* 修改说明：
//*
//**************************************************************/
//using EIP.Common.Controller.Attribute;
//using EIP.Common.Language.Resource;
//using EIP.Common.Models;
//using EIP.Common.Models.Dtos;
//using EIP.Common.Util;
//using EIP.System.Logic;
//using EIP.System.Models.Entities;
//using Microsoft.AspNetCore.Authorization;
//using Microsoft.AspNetCore.Mvc;
//using System;
//using System.Collections.Generic;
//using System.ComponentModel;
//using System.Linq;
//using System.Threading.Tasks;
//using SystemIo = System.IO;

//namespace EIP.System.Controller
//{
//    /// <summary>
//    /// 文件存储表
//    /// </summary>
//    public class FileController : BaseSystemController
//    {
//        #region 构造函数
//        private readonly ISystemFileLogic _systemFileLogic;

//        /// <summary>
//        /// 文件存储表构造函数
//        /// </summary>
//        /// <param name="systemFileLogic"></param>
//        public FileController(ISystemFileLogic systemFileLogic)
//        {
//            _systemFileLogic = systemFileLogic;
//        }

//        #endregion

//        #region 方法
//        /// <summary>
//        /// 一次性获取
//        /// </summary>
//        /// <returns></returns>
//        [HttpGet]
//        [CreateBy("孙泽伟")]
//        [Description("文件存储表-方法-列表-一次性获取")]
//        [Route("/system/file/query")]
//        public async Task<JsonResult> FindLoadonce()
//        {
//            return JsonForGridLoadOnce(await _systemFileLogic.FindAllAsync());
//        }

//        /// <summary>
//        /// 根据Id获取
//        /// </summary>
//        /// <param name="input">主键信息</param>
//        /// <returns></returns>
//        [HttpGet]
//        [CreateBy("孙泽伟")]
//        [Description("文件存储表-方法-编辑-根据Id获取")]
//        [Route("/system/file/{id}")]
//        public async Task<JsonResult> FindById(IdInput input)
//        {
//            return Json(await _systemFileLogic.FindByIdAsync(input.Id));
//        }

//        /// <summary>
//        /// 保存
//        /// </summary>
//        /// <param name="input">主键信息</param>
//        /// <returns></returns>
//        [HttpPost]
//        [CreateBy("孙泽伟")]
//        [Description("文件存储表-方法-编辑-保存")]
//        [Route("/system/file/save")]
//        public async Task<JsonResult> Save(SystemFile input)
//        {
//            return Json(await _systemFileLogic.Save(input));
//        }

//        /// <summary>
//        /// 删除
//        /// </summary>
//        /// <param name="input">主键集合</param>
//        /// <returns></returns>
//        [HttpPost]
//        [CreateBy("孙泽伟")]
//        [Description("文件存储表-方法-列表-删除")]
//        [Route("/system/file/{id}")]
//        public async Task<JsonResult> Delete(IdInput<string> input)
//        {
//            return Json(await _systemFileLogic.DeleteByIdsAsync(input.Id));
//        }

//        /// <summary>
//        /// 上传文件
//        /// </summary>
//        /// <returns></returns>
//        [HttpPost]
//        [CreateBy("孙泽伟")]
//        [Route("/system/file/upload")]
//        public async Task<JsonResult> UploadFile()
//        {
//            OperateStatus<IList<Guid>> operateStatus = new OperateStatus<IList<Guid>>();
//            try
//            {
//                IList<Guid> fileIds = new List<Guid>();
//                var files = Request.Form.Files;
//                if (files.Any())
//                {
//                    foreach (var file in files)
//                    {
//                        var uploadPath = ConfigurationUtil.GetUploadPath();
//                        if (!SystemIo.Directory.Exists(uploadPath + $"/{CurrentUser.UserId}"))
//                        {
//                            SystemIo.Directory.CreateDirectory(uploadPath + $"/{CurrentUser.UserId}");
//                        }
//                        string path = $"/{CurrentUser.UserId}/" + Guid.NewGuid() + SystemIo.Path.GetExtension(file.FileName);
//                        string filename = uploadPath + path;
//                        using (SystemIo.FileStream fs = SystemIo.File.Create(filename))
//                        {
//                            file.CopyTo(fs);
//                            fs.Flush();
//                            SystemFile systemFile = new SystemFile
//                            {
//                                Name = file.FileName,
//                                Path = path,
//                                ContentType = file.ContentType,
//                                Size = file.Length / 1024,
//                                CreateTime = DateTime.Now,
//                                CreateUserId = CurrentUser.UserId
//                            };
//                            var insertResult = await _systemFileLogic.InsertAsync(systemFile);
//                            if (insertResult.Code == ResultCode.Ok)
//                                fileIds.Add(systemFile.FileId);
//                        }
//                    }
//                    operateStatus.Data = fileIds;
//                    operateStatus.Code = ResultCode.Ok;
//                    operateStatus.Msg = Chs.Successful;
//                }
//                else
//                {
//                    operateStatus.Code = ResultCode.Ok;
//                    operateStatus.Msg = Chs.Successful;
//                }
//            }
//            catch (Exception ex)
//            {
//                operateStatus.Msg = ex.Message;
//            }
//            return Json(operateStatus);
//        }

//        /// <summary>
//        /// 修改上传文件:如修改头像图片等
//        /// </summary>
//        /// <returns></returns>
//        [HttpPut]
//        [CreateBy("孙泽伟")]
//        [Route("/system/file/edit")]
//        public async Task<JsonResult> EditUpdateFile(IdInput<int> input)
//        {
//            OperateStatus<IList<Guid>> operateStatus = new OperateStatus<IList<Guid>>();
//            try
//            {
//                IList<Guid> fileIds = new List<Guid>();
//                var files = Request.Form.Files;
//                if (files.Any())
//                {
//                    foreach (var file in files)
//                    {
//                        var uploadPath = ConfigurationUtil.GetUploadPath();
//                        if (!SystemIo.Directory.Exists(uploadPath + $"/{CurrentUser.UserId}"))
//                        {
//                            SystemIo.Directory.CreateDirectory(uploadPath + $"/{CurrentUser.UserId}");
//                        }
//                        string path = $"/{CurrentUser.UserId}/" + Guid.NewGuid() + SystemIo.Path.GetExtension(file.FileName);
//                        string filename = uploadPath + path;
//                        using (SystemIo.FileStream fs = SystemIo.File.Create(filename))
//                        {
//                            file.CopyTo(fs);
//                            fs.Flush();
//                            var systemFile = await _systemFileLogic.FindByIdAsync(input.Id);
//                            systemFile.Name = file.FileName;
//                            systemFile.Path = path;
//                            systemFile.ContentType = file.ContentType;
//                            systemFile.Size = file.Length / 1024;
//                            var result = await _systemFileLogic.UpdateAsync(systemFile);
//                            if (result.Code == ResultCode.Ok)
//                            {
//                                //判断图片类型
//                                switch (systemFile.Type)
//                                {
//                                    case 1://商品图片

//                                        break;
//                                    case 2://商家头像

//                                        break;
//                                }
//                            }
//                            fileIds.Add(systemFile.FileId);
//                        }
//                    }
//                    operateStatus.Data = fileIds;
//                    operateStatus.Code = ResultCode.Ok;
//                    operateStatus.Msg = Chs.Successful;
//                }
//                else
//                {
//                    operateStatus.Code = ResultCode.Ok;
//                    operateStatus.Msg = Chs.Successful;
//                }
//            }
//            catch (Exception ex)
//            {
//                operateStatus.Msg = ex.Message;
//            }
//            return Json(operateStatus);
//        }

//        /// <summary>
//        /// 获取文件
//        /// </summary>
//        /// <returns></returns>
//        [HttpGet]
//        [CreateBy("孙泽伟")]
//        [Route("/system/file/ids")]
//        public async Task<JsonResult> FindFile(IdInput<string> input)
//        {
//            return Json(await _systemFileLogic.FindFile(input));
//        }

//        /// <summary>
//        /// 获取文件
//        /// </summary>
//        /// <returns></returns>
//        [HttpGet]
//        [CreateBy("孙泽伟")]
//        [Route("/system/file/correlation")]
//        public async Task<JsonResult> FindFileByCorrelationId(IdInput<string> input)
//        {
//            return Json(await _systemFileLogic.FindFileByCorrelationId(input));
//        }


//        /// <summary>
//        /// 获取文件
//        /// </summary>
//        /// <returns></returns>
//        [HttpGet]
//        [CreateBy("孙泽伟")]
//        [Route("/system/file/image")]
//        public async Task<JsonResult> FindImage(IdInput input)
//        {
//            OperateStatus<string> operateStatus = new OperateStatus<string>();
//            var file = await _systemFileLogic.FindByIdAsync(input.Id);
//            if (file != null)
//            {
//                var path = ConfigurationUtil.GetFileServer() + "upload" + file.Path;
//                operateStatus.Data = path;
//                operateStatus.Msg = Chs.Successful;
//                operateStatus.Code = ResultCode.Ok;
//            }
//            return Json(operateStatus);
//        }

//        /// <summary>
//        /// 下载文件
//        /// </summary>
//        /// <param name="input"></param>
//        /// <returns></returns>
//        [AllowAnonymous]
//        [HttpPost]
//        [HttpGet]
//        [CreateBy("孙泽伟")]
//        [Route("/system/file/download")]
//        public async Task<IActionResult> DownLoadFile(IdInput input)
//        {
//            var fileInfo = await _systemFileLogic.FindByIdAsync(input.Id);
//            var uploadPath = ConfigurationUtil.GetUploadPath();
//            string filename = uploadPath + fileInfo.Path;
//            var stream = SystemIo.File.OpenRead(filename);
//            return File(stream, fileInfo.ContentType, fileInfo.Name);
//        }

//        /// <summary>
//        /// 上传文件
//        /// </summary>
//        /// <returns></returns>
//        [HttpPost]
//        [CreateBy("孙泽伟")]
//        [RequestSizeLimit(100_000_000)]//最大100m左右
//        [Route("/system/file/uploadform")]
//        public async Task<JsonResult> UploadFileForm(FileUploadInput input)
//        {
//            OperateStatus<IList<SystemFile>> operateStatus = new OperateStatus<IList<SystemFile>>();
//            try
//            {
//                IList<SystemFile> instanceFiles = new List<SystemFile>();
//                var files = Request.Form.Files;
//                var basePath = $"/system/file/{CurrentUser.UserId}";
//                if (files.Any())
//                {
//                    var uploadPath = ConfigurationUtil.GetUploadPath();
//                    if (!SystemIo.Directory.Exists(uploadPath + basePath))
//                    {
//                        SystemIo.Directory.CreateDirectory(uploadPath + basePath);
//                    }
//                    foreach (var file in files)
//                    {
//                        string path = basePath + "/" + Guid.NewGuid() + SystemIo.Path.GetExtension(file.FileName);
//                        string filename = uploadPath + path;
//                        using (SystemIo.FileStream fs = SystemIo.File.Create(filename))
//                        {
//                            file.CopyTo(fs);
//                            fs.Flush();
//                            instanceFiles.Add(new SystemFile
//                            {
//                                IsDelete = false,
//                                Name = file.FileName,
//                                Path = path,
//                                Extension = SystemIo.Path.GetExtension(file.FileName),
//                                ContentType = file.ContentType,
//                                Size = file.Length / 1024,
//                                CreateTime = DateTime.Now,
//                                CorrelationId = input.CorrelationId,
//                                CreateUserId = CurrentUser.UserId
//                            });
//                        }
//                        if (input.Single)
//                        {
//                            await _systemFileLogic.DeleteFileByRelationId(new IdInput<string>(input.CorrelationId));
//                        }
//                        var result = await _systemFileLogic.UploadFile(instanceFiles);
//                    }
//                    operateStatus.Data = instanceFiles;
//                    operateStatus.Code = ResultCode.Ok;
//                    operateStatus.Msg = Chs.Successful;
//                }
//                else
//                {
//                    operateStatus.Code = ResultCode.Ok;
//                    operateStatus.Msg = Chs.Successful;
//                }
//            }
//            catch (Exception ex)
//            {
//                operateStatus.Msg = ex.Message;
//            }
//            return Json(operateStatus);
//        }

//        /// <summary>
//        /// 删除文件
//        /// </summary>
//        /// <returns></returns>
//        [AllowAnonymous]
//        [HttpPost]
//        [CreateBy("孙泽伟")]
//        [Route("/system/file/delete")]
//        public async Task<JsonResult> DeleteFile(IdInput input)
//        {
//            return Json(await _systemFileLogic.DeleteFile(input));
//        }

//        /// <summary>
//        /// 文件上传，返回文件存储的相对路径数组
//        /// </summary>
//        /// <param name="input"></param>
//        /// <returns></returns>
//        [AllowAnonymous]
//        [HttpPost]
//        [CreateBy("孙泽伟")]
//        [Remark("文件上传，返回文件存储的相对路径数组", RemarkFrom.System)]
//        [RequestSizeLimit(100_000_000)]//最大100m左右
//        [Route("/system/file/uploadreturnpath")]
//        public JsonResult UploadFileFormReurnPaths(FileUploadInput input)
//        {
//            OperateStatus<List<string>> operateStatus = new OperateStatus<List<string>>();
//            try
//            {
//                var files = Request.Form.Files;
//                if (files.Any())
//                {
//                    string RelativePath = "Other";
//                    if (!string.IsNullOrEmpty(input.RelativePath))
//                        RelativePath = input.RelativePath;
//                    RelativePath = "/" + RelativePath + "/" + CurrentUser.UserId.ToString();
//                    var uploadPath = ConfigurationUtil.GetUploadPath();
//                    string tempPath = uploadPath + RelativePath;
//                    if (!SystemIo.Directory.Exists(tempPath))
//                    {
//                        SystemIo.Directory.CreateDirectory(tempPath);
//                    }
//                    List<string> filePathList = new List<string>();
//                    foreach (var file in files)
//                    {
//                        string fileName = Guid.NewGuid().ToString("N") + SystemIo.Path.GetExtension(file.FileName);
//                        string filePath = tempPath + "/" + fileName;
//                        using (SystemIo.FileStream fs = SystemIo.File.Create(filePath))
//                        {
//                            file.CopyTo(fs);
//                            fs.Flush();
//                        }
//                        string RelativePathFile = "/" + RelativePath + "/" + fileName;
//                        filePathList.Add(RelativePathFile);
//                    }
//                    operateStatus.Data = filePathList;
//                    operateStatus.Code = ResultCode.Ok;
//                    operateStatus.Msg = Chs.Successful;
//                }
//                else
//                {
//                    operateStatus.Code = ResultCode.Ok;
//                    operateStatus.Msg = Chs.Successful;
//                }
//            }
//            catch (Exception ex)
//            {
//                operateStatus.Msg = ex.Message;
//            }
//            return Json(operateStatus);
//        }
//        #endregion
//    }
//}
